package cn.iocoder.yudao.module.biz.controller.admin.contract.autocode.rule;

import cn.iocoder.yudao.module.biz.controller.admin.contract.autocode.rule.vo.AutoCodeRulePageReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.contract.autocode.rule.vo.AutoCodeRuleRespVO;
import cn.iocoder.yudao.module.biz.controller.admin.contract.autocode.rule.vo.AutoCodeRuleSaveReqVO;
import cn.iocoder.yudao.module.biz.dal.dataobject.contract.autocode.AutoCodeRuleDO;
import cn.iocoder.yudao.module.biz.service.contract.autocode.rule.AutoCodeRuleService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 编码生成规则")
@RestController
@RequestMapping("/auto/code-rule")
@Validated
public class AutoCodeRuleController {

    @Resource
    private AutoCodeRuleService codeRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建编码生成规则")
    @PreAuthorize("@ss.hasPermission('auto:code-rule:create')")
    public CommonResult<Long> createCodeRule(@Valid @RequestBody AutoCodeRuleSaveReqVO createReqVO) {
        return success(codeRuleService.createCodeRule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新编码生成规则")
    @PreAuthorize("@ss.hasPermission('auto:code-rule:update')")
    public CommonResult<Boolean> updateCodeRule(@Valid @RequestBody AutoCodeRuleSaveReqVO updateReqVO) {
        codeRuleService.updateCodeRule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除编码生成规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('auto:code-rule:delete')")
    public CommonResult<Boolean> deleteCodeRule(@RequestParam("id") Long id) {
        codeRuleService.deleteCodeRule(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除编码生成规则")
                @PreAuthorize("@ss.hasPermission('auto:code-rule:delete')")
    public CommonResult<Boolean> deleteCodeRuleList(@RequestParam("ids") List<Long> ids) {
        codeRuleService.deleteCodeRuleListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得编码生成规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('auto:code-rule:query')")
    public CommonResult<AutoCodeRuleRespVO> getCodeRule(@RequestParam("id") Long id) {
        AutoCodeRuleDO codeRule = codeRuleService.getCodeRule(id);
        return success(BeanUtils.toBean(codeRule, AutoCodeRuleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得编码生成规则分页")
    @PreAuthorize("@ss.hasPermission('auto:code-rule:query')")
    public CommonResult<PageResult<AutoCodeRuleRespVO>> getCodeRulePage(@Valid AutoCodeRulePageReqVO pageReqVO) {
        PageResult<AutoCodeRuleDO> pageResult = codeRuleService.getCodeRulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AutoCodeRuleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出编码生成规则 Excel")
    @PreAuthorize("@ss.hasPermission('auto:code-rule:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCodeRuleExcel(@Valid AutoCodeRulePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AutoCodeRuleDO> list = codeRuleService.getCodeRulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "编码生成规则.xls", "数据", AutoCodeRuleRespVO.class,
                        BeanUtils.toBean(list, AutoCodeRuleRespVO.class));
    }

}